package com.itheima.web;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
@WebServlet("/req1")
public class RequestDemo1 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String method = req.getMethod();//获取请求方式：GET
        System.out.println(method);

        String contextPath = req.getContextPath();//获取虚拟目录（项目访问路径）/Tomcat_test
        System.out.println(contextPath);

        StringBuffer requestURL = req.getRequestURL();//获取URL（统一资源定位符）http://localhost:8080/Tomcat_test/req1
//        System.out.println(requestURL);
        System.out.println(requestURL.toString());

        String requestURI = req.getRequestURI();//获取URI（统一资源标识符）/Tomcat_test/req1
        System.out.println(requestURI);

        String queryString = req.getQueryString();//获取请求参数（GET方式）username=zhangsan&password=123(输入给网址就行)
        System.out.println(queryString);




        //------------------------
        String header = req.getHeader("user-agent");//获取请求头：user-agent：浏览器的版本信息
        System.out.println(header);



    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取post 请求体：请求参数
        //1.获取字符输入流
        BufferedReader reader = req.getReader();
        //2.读取数据
        String line = reader.readLine();
        System.out.println(line);
    }
}
